/**
 * 
 */
package com.usc.entme;

import static android.opengl.GLES10.GL_CCW;
import static android.opengl.GLES10.GL_COLOR_BUFFER_BIT;
import static android.opengl.GLES10.GL_DEPTH_BUFFER_BIT;
import static android.opengl.GLES10.GL_DEPTH_TEST;
import static android.opengl.GLES10.GL_DITHER;
import static android.opengl.GLES10.GL_FASTEST;
import static android.opengl.GLES10.GL_FLOAT;
import static android.opengl.GLES10.GL_MODELVIEW;
import static android.opengl.GLES10.GL_MODULATE;
import static android.opengl.GLES10.GL_PERSPECTIVE_CORRECTION_HINT;
import static android.opengl.GLES10.GL_PROJECTION;
import static android.opengl.GLES10.GL_REPEAT;
import static android.opengl.GLES10.GL_SMOOTH;
import static android.opengl.GLES10.GL_TEXTURE_2D;
import static android.opengl.GLES10.GL_TEXTURE_COORD_ARRAY;
import static android.opengl.GLES10.GL_TEXTURE_ENV;
import static android.opengl.GLES10.GL_TEXTURE_ENV_MODE;
import static android.opengl.GLES10.GL_TEXTURE_WRAP_S;
import static android.opengl.GLES10.GL_TEXTURE_WRAP_T;
import static android.opengl.GLES10.GL_TRIANGLE_STRIP;
import static android.opengl.GLES10.GL_UNSIGNED_SHORT;
import static android.opengl.GLES10.GL_VERTEX_ARRAY;
import static android.opengl.GLES10.glActiveTexture;
import static android.opengl.GLES10.glBindTexture;
import static android.opengl.GLES10.glClear;
import static android.opengl.GLES10.glDisable;
import static android.opengl.GLES10.glDrawElements;
import static android.opengl.GLES10.glEnable;
import static android.opengl.GLES10.glEnableClientState;
import static android.opengl.GLES10.glFrontFace;
import static android.opengl.GLES10.glFrustumf;
import static android.opengl.GLES10.glHint;
import static android.opengl.GLES10.glLoadIdentity;
import static android.opengl.GLES10.glMatrixMode;
import static android.opengl.GLES10.glShadeModel;
import static android.opengl.GLES10.glTexCoordPointer;
import static android.opengl.GLES10.glTexEnvx;
import static android.opengl.GLES10.glTexParameterx;
import static android.opengl.GLES10.glVertexPointer;
import static android.opengl.GLES10.glViewport;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
import android.opengl.GLUtils;
import android.os.SystemClock;
import android.util.Log;

/**
 * @author pranav
 *
 */
public class ResultRenderer implements Renderer{

	private Context mContext;
	private Square mSquare;
	private Square mSquare1;
	private Square mSquare2;
	private Square mSquare3;
	private Square mSquare4;
	private Square mSquare5;
	private Square mSquare6;
	private Square mSquare7;
	private Square mSquare8;
	private Square mSquare9;
	private TextureLoader mTextureLoader;	 
	private String mCallingClassName;
	private int[] textures;

	public float zTransition;
	public float xEyeLocation;
	public float yEyeLocation;
	public float zEyeLocation;

	public static String TAG = "3D";	


	private float[] lightAmbient	= { 0.2f, 0.0f, 0.0f, 1.0f };
	private float[] lightDiffuse	= { 0.5f, 0.0f, 0.0f, 1.0f };
	private float[] lightSpecular	= { 1.0f, 1.0f, 1.0f, 1.0f };
	private float[] matAmbient	= { 1.0f, 1.0f, 1.0f, 1.0f };
	private float[] matDiffuse	= { 1.0f, 1.0f, 1.0f, 1.0f };
	private float[] matSpecular	= { 1.0f, 1.0f, 1.0f, 1.0f };
	private float[] lightPosition = { 2.0f, 2.0f, 3.0f, 0.0f };
	private float[] lightDirection = { -2.0f, -2.0f, -3.0f };


	private FloatBuffer lightAmbientBuf;	
	private FloatBuffer lightDiffuseBuf;	
	private FloatBuffer lightSpecularBuf;	
	private FloatBuffer matAmbientBuf;
	private FloatBuffer matDiffuseBuf;
	private FloatBuffer matSpecularBuf;
	private FloatBuffer lightPositionBuf;
	private FloatBuffer lightDirectionBuf;
	


	/*
	 * implementation for loading texture
	 */
	public interface TextureLoader {

		/**
		 * this is the method to test the another approach for the renderer image loading
		 * 
		 * @param gl
		 * @param context
		 */
		void load(GL10 gl, Context context);
	}

	/*
	 * This class is used to load the resultItems which are fetched from the sites
	 * It would concatenate the images + textual data and represent it on the square planes on GLSurfaceView
	 * 
	 */
	private class ResultTextureLoader implements TextureLoader {

		@Override
		public void load(GL10 gl, Context context) {
			
			Bitmap bitmap,bitmap00 = null,bitmap1,bitmap01 = null,bitmap2 ,bitmap02 = null,bitmap3 ,bitmap03 = null,bitmap4 , bitmap04 = null, 
					bitmap5 ,bitmap05 = null, bitmap6 , bitmap06 = null,bitmap7 ,bitmap07 = null,bitmap8 , bitmap08 = null,bitmap9 ,bitmap09 = null;


			if(mCallingClassName.contains("Movie")){
				bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.brkingdawn);
				bitmap00 = Bitmap.createScaledBitmap(bitmap, 256, 256, false);

				bitmap1 = BitmapFactory.decodeResource(context.getResources(),R.drawable.immortal);
				bitmap01 = Bitmap.createScaledBitmap(bitmap1, 256, 256, false);

				bitmap2 = BitmapFactory.decodeResource(context.getResources(),R.drawable.happyfeet);
				bitmap02 = Bitmap.createScaledBitmap(bitmap2, 256, 256, false);

				bitmap3 = BitmapFactory.decodeResource(context.getResources(),R.drawable.pussinboots);
				bitmap03 = Bitmap.createScaledBitmap(bitmap3, 256, 256, false);

				bitmap4 = BitmapFactory.decodeResource(context.getResources(),R.drawable.muppets);
				bitmap04 = Bitmap.createScaledBitmap(bitmap4, 256, 256, false);

				bitmap5 = BitmapFactory.decodeResource(context.getResources(),R.drawable.marilyn);
				bitmap05 = Bitmap.createScaledBitmap(bitmap5, 256, 256, false);

				bitmap6 = BitmapFactory.decodeResource(context.getResources(),R.drawable.hugo);
				bitmap06 = Bitmap.createScaledBitmap(bitmap6, 256, 256, false);

				bitmap7 = BitmapFactory.decodeResource(context.getResources(),R.drawable.dangerousmethod);
				bitmap07 = Bitmap.createScaledBitmap(bitmap7, 256, 256, false);

				bitmap8 = BitmapFactory.decodeResource(context.getResources(),R.drawable.artist);
				bitmap08 = Bitmap.createScaledBitmap(bitmap8, 256, 256, false);

				bitmap9 = BitmapFactory.decodeResource(context.getResources(),R.drawable.arthurchristmas);
				bitmap09 = Bitmap.createScaledBitmap(bitmap9, 256, 256, false);
				
			} else if(mCallingClassName.contains("News")){
				
				bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.nb1);
				bitmap00 = Bitmap.createScaledBitmap(bitmap, 256, 256, false);

				bitmap1 = BitmapFactory.decodeResource(context.getResources(),R.drawable.nb2);
				bitmap01 = Bitmap.createScaledBitmap(bitmap1, 256, 256, false);

				bitmap2 = BitmapFactory.decodeResource(context.getResources(),R.drawable.nb3);
				bitmap02 = Bitmap.createScaledBitmap(bitmap2, 256, 256, false);

				bitmap3 = BitmapFactory.decodeResource(context.getResources(),R.drawable.nb4);
				bitmap03 = Bitmap.createScaledBitmap(bitmap3, 256, 256, false);

				bitmap4 = BitmapFactory.decodeResource(context.getResources(),R.drawable.nb5);
				bitmap04 = Bitmap.createScaledBitmap(bitmap4, 256, 256, false);

				bitmap5 = BitmapFactory.decodeResource(context.getResources(),R.drawable.nb6);
				bitmap05 = Bitmap.createScaledBitmap(bitmap5, 256, 256, false);

				bitmap6 = BitmapFactory.decodeResource(context.getResources(),R.drawable.nb7);
				bitmap06 = Bitmap.createScaledBitmap(bitmap6, 256, 256, false);

				bitmap7 = BitmapFactory.decodeResource(context.getResources(),R.drawable.nb8);
				bitmap07 = Bitmap.createScaledBitmap(bitmap7, 256, 256, false);

				bitmap8 = BitmapFactory.decodeResource(context.getResources(),R.drawable.nb9);
				bitmap08 = Bitmap.createScaledBitmap(bitmap8, 256, 256, false);

				bitmap9 = BitmapFactory.decodeResource(context.getResources(),R.drawable.nb10);
				bitmap09 = Bitmap.createScaledBitmap(bitmap9, 256, 256, false);
				
			} else if(mCallingClassName.contains("Sports")){
				
				bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.sy1);
				bitmap00 = Bitmap.createScaledBitmap(bitmap, 256, 256, false);

				bitmap1 = BitmapFactory.decodeResource(context.getResources(),R.drawable.sy2);
				bitmap01 = Bitmap.createScaledBitmap(bitmap1, 256, 256, false);

				bitmap2 = BitmapFactory.decodeResource(context.getResources(),R.drawable.sy3);
				bitmap02 = Bitmap.createScaledBitmap(bitmap2, 256, 256, false);

				bitmap3 = BitmapFactory.decodeResource(context.getResources(),R.drawable.sy4);
				bitmap03 = Bitmap.createScaledBitmap(bitmap3, 256, 256, false);

				bitmap4 = BitmapFactory.decodeResource(context.getResources(),R.drawable.sy5);
				bitmap04 = Bitmap.createScaledBitmap(bitmap4, 256, 256, false);
			}
			
				bitmap = bitmap00;
				bitmap1 = bitmap01;
				bitmap2 = bitmap02;
				bitmap3 = bitmap03;
				bitmap4 = bitmap04;
				bitmap5 = bitmap05;
				bitmap6 = bitmap06;
				bitmap7 = bitmap07;
				bitmap8 = bitmap08;
				bitmap9 = bitmap09;

			//Generate there texture pointer
			textures = new int[10];
			gl.glGenTextures(10, textures, 0);

			//generateTexture(gl, bitmap);
			
			//Create  Texture and bind it to texture 0
			gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
			GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

			//Clean up
			bitmap.recycle();
			//bitmap = null;

			//Create  Texture and bind it to texture 1
			gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[1]);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
			GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap1, 0);

			//Clean up
			bitmap1.recycle();
			//bitmap1 = null;

			//Create  Texture and bind it to texture 2
			gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[2]);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
			GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap2, 0);

			//Clean up
			bitmap2.recycle();
			//bitmap2 = null;
			
			gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[3]);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
			GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap3, 0);

			//Clean up
			bitmap3.recycle();
			//bitmap3 = null;

			//Create  Texture and bind it to texture 0
			gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[4]);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
			GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap4, 0);
			//Clean up
			bitmap4.recycle();
			//bitmap4 = null;
			
			if(!mCallingClassName.contains("Sports")){				
			
			//Create  Texture and bind it to texture 0
			gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[5]);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
			GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap5, 0);

			//Clean up
			bitmap5.recycle();
			//bitmap5 = null;
			
			//Create  Texture and bind it to texture 0
			gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[6]);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
			GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap6, 0);

			//Clean up
			bitmap6.recycle();
			//bitmap6 = null;
			
			//Create  Texture and bind it to texture 0
			gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[7]);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
			GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap7, 0);

			//Clean up
			bitmap7.recycle();
			//bitmap7 = null;
			
			//Create  Texture and bind it to texture 0
			gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[8]);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
			GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap8, 0);

			//Clean up
			bitmap8.recycle();
			//bitmap8 = null;
			//Create  Texture and bind it to texture 0
			gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[9]);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
			GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap9, 0);

			//Clean up
			bitmap9.recycle();
			//bitmap9 = null;
			
			} // end of sports loop for the bitmaps

		}

		
		/**
		 * @param gl
		 * @param bitmap
		 */
		
		/*
		private void generateTexture(GL10 gl, Bitmap bitmap) {
			//Create  Texture and bind it to texture 0
			gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
			GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

			//Clean up
			bitmap.recycle();
			bitmap = null;
		}
		*/
		
	}


	public ResultRenderer(Context context) {
		init(context, new ResultTextureLoader(),"classnamenotset");
	}

	public ResultRenderer(Context context, TextureLoader loader) {
		init(context, loader,"classnamenotset");
	}

	public ResultRenderer(Context context, String className) {
		init(context, new ResultTextureLoader(), className);
	}

	private void init(Context context, TextureLoader loader, String callingClassName) {
		mContext = context;
		mSquare = new Square(0.0f);
		mSquare1 = new Square(2.0f);
		mSquare2 = new Square(4.0f);
		mSquare3 = new Square(6.0f);
		mSquare4 = new Square(8.0f);
		mCallingClassName = callingClassName;
		if(!mCallingClassName.contains("Sports")){
		mSquare5 = new Square(10.0f);
		mSquare6 = new Square(12.0f);
		mSquare7 = new Square(14.0f);
		mSquare8 = new Square(16.0f);
		mSquare9 = new Square(18.0f);
		}
		zTransition = 0.0f;
		mTextureLoader = loader;      	

	}

	@Override
	public void onDrawFrame(GL10 gl) {
		
		
		/*
		 * Lighting Code Starts 
		
		 ByteBuffer lightAmbientbb = ByteBuffer.allocateDirect(lightAmbient.length * 4);
		 lightAmbientbb.order(ByteOrder.nativeOrder());
		 lightAmbientBuf = lightAmbientbb.asFloatBuffer();
		 
		 ByteBuffer lightDiffusebb = ByteBuffer.allocateDirect(lightDiffuse.length * 4);
		 lightDiffusebb.order(ByteOrder.nativeOrder());
		 lightDiffuseBuf = lightDiffusebb.asFloatBuffer();
		 
		 ByteBuffer lightSpecularbb = ByteBuffer.allocateDirect(lightSpecular.length * 4);
		 lightSpecularbb.order(ByteOrder.nativeOrder());
		 lightSpecularBuf = lightSpecularbb.asFloatBuffer();
		 
		 ByteBuffer matAmbientbb = ByteBuffer.allocateDirect(matAmbient.length * 4);
		 matAmbientbb.order(ByteOrder.nativeOrder());
		 matAmbientBuf = matAmbientbb.asFloatBuffer();
		 
		 ByteBuffer matDiffusebb = ByteBuffer.allocateDirect(matDiffuse.length * 4);
		 matDiffusebb.order(ByteOrder.nativeOrder());
		 matDiffuseBuf = matDiffusebb.asFloatBuffer();
		 
		 ByteBuffer matSpecularbb = ByteBuffer.allocateDirect(matSpecular.length * 4);
		 matSpecularbb.order(ByteOrder.nativeOrder());
		 matSpecularBuf = matSpecularbb.asFloatBuffer();
		 
		 ByteBuffer lightPositionbb = ByteBuffer.allocateDirect(lightPosition.length * 4);
		 lightPositionbb.order(ByteOrder.nativeOrder());
		 lightPositionBuf = lightPositionbb.asFloatBuffer();
		 
		 ByteBuffer lightDirectionbb = ByteBuffer.allocateDirect(lightDirection.length * 4);
		 lightDirectionbb.order(ByteOrder.nativeOrder());
		 lightDirectionBuf = lightDirectionbb.asFloatBuffer();
		 		
		 

		 lightAmbientBuf.put(lightAmbient);
		 lightAmbientBuf.position(0);
		 lightDiffuseBuf.put(lightDiffuse);
		 lightDiffuseBuf.position(0);
		 lightSpecularBuf.put(lightSpecular);
		 lightSpecularBuf.position(0);
		 matDiffuseBuf.put(matDiffuse);
		 matDiffuseBuf.position(0);
		 matSpecularBuf.put(matSpecular);
		 matSpecularBuf.position(0);
		 lightPositionBuf.put(lightPosition);
		 lightPositionBuf.position(0);
		 lightDirectionBuf.put(lightDirection);
		 lightDirectionBuf.position(0);
		 matAmbientBuf.put(matAmbient);
		 matAmbientBuf.position(0);


		 gl.glEnable(GL10.GL_LIGHTING);
		 gl.glEnable(GL10.GL_LIGHT0);

		 gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, matAmbientBuf);
		 gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, matDiffuseBuf);
		 gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, matSpecularBuf);
		 gl.glMaterialf(GL10.GL_FRONT_AND_BACK, GL10.GL_SHININESS, 20.0f);

		 gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbientBuf);
		 gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuseBuf);
		 gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPECULAR, lightSpecularBuf);

		 gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPositionBuf);
		 gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPOT_DIRECTION, lightDirectionBuf);
		 gl.glLightf(GL10.GL_LIGHT0, GL10.GL_SPOT_CUTOFF, 1.2f);
		 gl.glLightf(GL10.GL_LIGHT0, GL10.GL_SPOT_EXPONENT, 20.0f);

		 gl.glEnable(GL10.GL_DEPTH_TEST);
		 gl.glDepthFunc(GL10.GL_LEQUAL);

		 gl.glEnable(GL10.GL_CULL_FACE);
		 gl.glShadeModel(GL_SMOOTH);



		 
		  * Lighting code ends
		  * 
		  */

		 /*
		  * By default, OpenGL enables features that improve quality
		  * but reduce performance. One might want to tweak that
		  * especially on software renderer.
		  */
		 glDisable(GL_DITHER);

		 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
				 GL_MODULATE);

		 /*
		  * Usually, the first thing one might want to do is to clear
		  * the screen. The most efficient way of doing this is to use
		  * glClear().
		  */

		 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		 /*
		  * Now we're ready to draw some 3D objects
		  */

		 glMatrixMode(GL_MODELVIEW);
		 glLoadIdentity();     


		 // GLU.gluLookAt(gl, 2, -2, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f); 
		 //GLU.gluLookAt(gl, 2, -2, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
		//Log.d(TAG, "EyeLocation x = " + xEyeLocation + " y = " + yEyeLocation);
		 GLU.gluLookAt(gl, xEyeLocation, yEyeLocation, -5.7f, 0f, 0f, 0f, 0f, -3.0f, 0.0f);  


		 glEnableClientState(GL_VERTEX_ARRAY);
		 glEnableClientState(GL_TEXTURE_COORD_ARRAY);

		 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
				 GL_REPEAT);
		 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
				 GL_REPEAT);


		 long time = SystemClock.uptimeMillis() % 4000L;
		 float angle = 0.090f * ((int) time);
		 //glRotatef(angle, 0, 0, 1.0f);
		 //gl.glRotatef(180, 0, 0, 1.0f);
		 gl.glTranslatef(0, 0, zTransition);           

		 glActiveTexture(textures[0]);
		 glBindTexture(GL_TEXTURE_2D,textures[0]);
		 mSquare.draw(gl);        

		 glActiveTexture(textures[1]);
		 glBindTexture(GL_TEXTURE_2D,textures[1]);        
		 mSquare1.draw(gl);


		 glActiveTexture(textures[2]);
		 glBindTexture(GL_TEXTURE_2D,textures[2]);        
		 mSquare2.draw(gl); 

		 glActiveTexture(textures[3]);
		 glBindTexture(GL_TEXTURE_2D,textures[3]);        
		 mSquare3.draw(gl); 

		 glActiveTexture(textures[4]);
		 glBindTexture(GL_TEXTURE_2D,textures[4]);
		 mSquare4.draw(gl); 
		 
		 if(!mCallingClassName.contains("Sports")){
		 glActiveTexture(textures[5]);
		 glBindTexture(GL_TEXTURE_2D,textures[5]);
		 mSquare5.draw(gl);
		 
		 glActiveTexture(textures[6]);
		 glBindTexture(GL_TEXTURE_2D,textures[6]);
		 mSquare6.draw(gl);
		 
		 glActiveTexture(textures[7]);
		 glBindTexture(GL_TEXTURE_2D,textures[7]);
		 mSquare7.draw(gl);
		 
		 glActiveTexture(textures[8]);
		 glBindTexture(GL_TEXTURE_2D,textures[8]);
		 mSquare8.draw(gl);
		 
		 glActiveTexture(textures[9]);
		 glBindTexture(GL_TEXTURE_2D,textures[9]);
		 mSquare9.draw(gl);
		 }


	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {

		glViewport(0, 0, width, height);

		/*
		 * Set our projection matrix. This doesn't have to be done
		 * each time we draw, but usually a new projection needs to
		 * be set when the viewport is resized.
		 */

		float ratio = (float) width / height;

		glMatrixMode(GL_PROJECTION);        

		glLoadIdentity();

		// original value
		// glFrustumf(-ratio, ratio, -1, 1, 3, 7);
		glFrustumf(-ratio, ratio, -1, 1, 3, 15);
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {

		/*
		 * By default, OpenGL enables features that improve quality
		 * but reduce performance. One might want to tweak that
		 * especially on software renderer.
		 */
		glDisable(GL_DITHER);

		/*
		 * Some one-time OpenGL initialization can be made here
		 * probably based on features of this particular context
		 */
		glHint(GL_PERSPECTIVE_CORRECTION_HINT,
				GL_FASTEST);

		//We are clearing out the background color
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
		//glClearColor(.5f, .5f, .5f, 1);
		glShadeModel(GL_SMOOTH);
		glEnable(GL_DEPTH_TEST);
		glEnable(GL_TEXTURE_2D);

		mTextureLoader.load(gl,mContext);      

	}



	/**
	 * This is the private class which represent the 3DSquares;
	 * to display the content on.
	 * 
	 */

	static class Square {

		private final static int VERTS = 4;

		private FloatBuffer mFVertexBuffer;
		private FloatBuffer mTexBuffer;
		private ShortBuffer mIndexBuffer;

		public Square(float zoffset) {

			Log.d(TAG ,"Square create with Z offset : " + zoffset);

			ByteBuffer vbb = ByteBuffer.allocateDirect(VERTS * 3 * 4);
			vbb.order(ByteOrder.nativeOrder());
			mFVertexBuffer = vbb.asFloatBuffer();

			ByteBuffer tbb = ByteBuffer.allocateDirect(VERTS * 2 * 4);
			tbb.order(ByteOrder.nativeOrder());
			mTexBuffer = tbb.asFloatBuffer();

			ByteBuffer ibb = ByteBuffer.allocateDirect(VERTS * 2);
			ibb.order(ByteOrder.nativeOrder());
			mIndexBuffer = ibb.asShortBuffer();

			// A unit-sided equilateral triangle centered on the origin.
			float[] coords = {
					// X, Y, Z
					-0.5f, 0.5f, zoffset,
					-0.5f, -0.5f, zoffset,
					0.5f, 0.5f, zoffset,
					0.5f, -0.5f, zoffset
			};

			/*
            float[] coords = {
                    // X, Y, Z 
            		0.5f, -0.5f, zoffset,
                     0.5f, 0.5f, zoffset,
                     -0.5f, -0.5f, zoffset,
                     -0.5f, 0.5f, zoffset
            };*/

			for (int i = 0; i < VERTS; i++) {
				for(int j = 0; j < 3; j++) {
					mFVertexBuffer.put(coords[i*3+j] * 2.0f);
				}
			}

			for (int i = 0; i < VERTS; i++) {
				for(int j = 0; j < 2; j++) {

					Log.d(TAG, "TextureBuffer value[" +i+"][" +j+"] = " + coords[i*3+j] * 1.0f + 0.5f );
					mTexBuffer.put(coords[i*3+j] * 1.0f + 0.5f);                    
				}
			}

			for(int i = 0; i < VERTS; i++) {
				Log.d(TAG, "IndexBuffer value[" +i+"] = " + (short)i);
				mIndexBuffer.put((short) i);
			}

			/*
			 * 
			 * Another implementation for the squares;
			 * this would not make the texture to flip the reverse
			 * 
            float[] coords = {
					// X, Y, Z
					-0.5f, -0.5f, zoffset,
					0.5f, -0.5f, zoffset,
					-0.5f, 0.5f, zoffset,
					0.5f, 0.5f, zoffset
			};

			// Mapping coordinates for the vertices
			float textureCoordinates[] = { 0.0f, 1.0f, //
					1.0f, 1.0f, //
					0.0f, 0.0f, //
					1.0f, 0.0f, //
			};

			short[] indices = new short[] { 0, 1, 2, 1, 3, 2 };
			for (int i = 0; i < VERTS; i++) {
				for(int j = 0; j < 3; j++) {
					mFVertexBuffer.put(coords[i*3+j] * 2.0f);
				}
			}
			mTexBuffer.put(textureCoordinates);
			mIndexBuffer.put(indices);

			 *
			 *
			 */

			mFVertexBuffer.position(0);
			mTexBuffer.position(0);
			mIndexBuffer.position(0);
		}

		public void draw(GL10 gl) {

			glFrontFace(GL_CCW);
			glVertexPointer(3, GL_FLOAT, 0, mFVertexBuffer);
			glEnable(GL_TEXTURE_2D);
			glTexCoordPointer(2, GL_FLOAT, 0, mTexBuffer);
			glDrawElements(GL_TRIANGLE_STRIP, VERTS,
					GL_UNSIGNED_SHORT, mIndexBuffer);   


		}


	}




	static class SelectionSquare {

		private final static int VERTS = 4;

		private FloatBuffer mFVertexBuffer;
		private FloatBuffer mTexBuffer;
		private ShortBuffer mIndexBuffer;

		public SelectionSquare() {

			ByteBuffer vbb = ByteBuffer.allocateDirect(VERTS * 3 * 4);
			vbb.order(ByteOrder.nativeOrder());
			mFVertexBuffer = vbb.asFloatBuffer();

			ByteBuffer tbb = ByteBuffer.allocateDirect(VERTS * 2 * 4);
			tbb.order(ByteOrder.nativeOrder());
			mTexBuffer = tbb.asFloatBuffer();

			ByteBuffer ibb = ByteBuffer.allocateDirect(VERTS * 2);
			ibb.order(ByteOrder.nativeOrder());
			mIndexBuffer = ibb.asShortBuffer();

			// A unit-sided equilateral triangle centered on the origin.
			float[] coords = {
					// X, Y, Z
					-0.55f, 0.55f, 0.0f,
					-0.55f, -0.55f, 0.0f,
					0.55f, 0.55f, 0.0f,
					0.55f, -0.55f, 0.0f
			};


			for (int i = 0; i < VERTS; i++) {
				for(int j = 0; j < 3; j++) {
					mFVertexBuffer.put(coords[i*3+j] * 2.0f);
				}
			}

			for (int i = 0; i < VERTS; i++) {
				for(int j = 0; j < 2; j++) {

					Log.d(TAG, "TextureBuffer value[" +i+"][" +j+"] = " + coords[i*3+j] * 1.0f + 0.5f );
					mTexBuffer.put(coords[i*3+j] * 1.0f + 0.5f);                    
				}
			}

			for(int i = 0; i < VERTS; i++) {
				Log.d(TAG, "IndexBuffer value[" +i+"] = " + (short)i);
				mIndexBuffer.put((short) i);
			}        


			mFVertexBuffer.position(0);
			mTexBuffer.position(0);
			mIndexBuffer.position(0);
		}

		public void draw(GL10 gl) {

			glFrontFace(GL_CCW);
			glVertexPointer(3, GL_FLOAT, 0, mFVertexBuffer);
			glEnable(GL_TEXTURE_2D);
			glTexCoordPointer(2, GL_FLOAT, 0, mTexBuffer);
			glDrawElements(GL_TRIANGLE_STRIP, VERTS,
					GL_UNSIGNED_SHORT, mIndexBuffer);               

		}

	}


}
